//
//  LoopVisitorLoginView.swift
//  LoopWeibo
//
//  Created by loop安 on 2016/12/8.
//  Copyright © 2016年 loop. All rights reserved.
//

import UIKit
import SnapKit

//游客登录视图的是协议
@objc protocol LoopVisitorLoginViewDelegate:NSObjectProtocol {
    @objc optional func visitorWillLogin()
    
    func visitorWillRegist()
}

class LoopVisitorLoginView: UIView {

    weak var delegate:LoopVisitorLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(colorLiteralRed: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //外部修改图片和label的方法
    func changeInfo(title:String,imageName:String,isHome:Bool = false){
        circleV.image = UIImage(named: imageName)
        lblTip.text = title
        if isHome{
            circleAnimation()
        }else{
            bigHouseV.isHidden = true
            shadowV.isHidden = true
        }
    }
    
    //circle的动画方法
    private func circleAnimation(){
        let circle = CABasicAnimation(keyPath: "transform.rotation")
        circle.toValue = M_PI * 2
        circle.duration = 20
        circle.repeatCount = MAXFLOAT
        circle.isRemovedOnCompletion = false
        circleV.layer.add(circle, forKey: "circle")
    }
    
    private func setupUI() {
        loginBtn.addTarget(self, action: #selector(clickLoginBtn), for: .touchUpInside)
        registeBtn.addTarget(self, action: #selector(clickRegistBtn), for: .touchUpInside)
        
        //添加控件
        addSubview(circleV)
        addSubview(shadowV)
        addSubview(bigHouseV)
        addSubview(lblTip)
        addSubview(loginBtn)
        addSubview(registeBtn)
        
        //添加约束
        circleV.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        bigHouseV.snp.makeConstraints { (make) in
            make.center.equalTo(circleV)
        }
        shadowV.snp.makeConstraints { (make) in
            make.center.equalTo(circleV)
        }
        lblTip.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(circleV.snp.bottom)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(lblTip.snp.left)
            make.top.equalTo(lblTip.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        registeBtn.snp.makeConstraints { (make) in
            make.right.equalTo(lblTip.snp.right)
            make.top.equalTo(lblTip.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
    
    }
    
    //按钮的点击事件
    @objc private func clickLoginBtn(){
        delegate?.visitorWillLogin!()
    }
    
    @objc private func clickRegistBtn(){
        delegate?.visitorWillRegist()
    }
    
    //控件的懒加载
    private lazy var circleV:UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon") )
    
    private lazy var bigHouseV:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    private lazy var shadowV:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var lblTip:UILabel = {
    let lab = UILabel(title: "关注一些人,关注一些人,回到这里看看有什么惊喜", fontsize: 14, textColor: #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1))
        lab.numberOfLines = 0
        lab.textAlignment = .center
        lab.preferredMaxLayoutWidth = 220
    return lab
    }()
    
    private lazy var loginBtn:UIButton = {
    let btn = UIButton(title: "登录", fontSize: 14, textColor: #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1))
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    
    private lazy var registeBtn:UIButton = {
        let btn = UIButton(title: "注册", fontSize: 14, textColor: #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1))
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
}
